﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using jcc.Helpers;
namespace jcc
{
    public partial class dlgSelectPage : Form
    {
        public dlgSelectPage(string message, int level)
        {
            InitializeComponent();
            lblMessage.Text = message;
            visibleLevels = level;
            LoadFoldersDictionary();
            PopulateTree();
        }
        
        int visibleLevels = 0;
        Hashtable foldersDictionary;
     
        public folderWrapper SelectedFolder
        {
            get
            {
                try
                {
                    return (folderWrapper)treeView1.SelectedNode.Tag;
                }
                catch { return new folderWrapper(0); }
            }
        }
        private void PopulateTree()
        {
            treeView1.Nodes.Clear();
            foreach (DictionaryEntry entry in foldersDictionary)
            {
                folderWrapper folder = (folderWrapper)entry.Value;
                TreeNode node = new TreeNode();
                if (folder.Parent_ID == 0)
                {
                    node = MakeBranchNode(folder, 0);
                    treeView1.Nodes.Add(node);
                }
            }
            treeView1.TreeViewNodeSorter = new NodeSorter();
            treeView1.Sort();
        }

        TreeNode MakeBranchNode(folderWrapper folder, int level)
        {
            TreeNode node = new TreeNode();

            node.Tag = folder;
            node.Text = folder.Name;

            if (folder.Children.Count > 0)
            {
                level = level + 1;
                foreach (folderWrapper child in folder.Children)
                {

                    if (level < visibleLevels)
                    {
                        TreeNode childNode = MakeBranchNode(child, level);
                        node.Nodes.Add(childNode);
                    }
                }
            }
            return node;
        }
        private void LoadFoldersDictionary()
        {
            foldersDictionary = new Hashtable();


            jcc.net.metroeguide.Folder[] folders = jcc.StandardWebService.WebService.GetAllFolders(jcc.StandardWebService.GetKey());

            foreach(jcc.net.metroeguide.Folder folder in folders)
            {
                folderWrapper fold = new folderWrapper(folder.ID);
                fold.Name = folder.Name;
                fold.Parent_ID = folder.Parent_Folder_ID;

                if (foldersDictionary.ContainsKey(fold.Parent_ID))
                {
                    folderWrapper parentFolder = (folderWrapper)foldersDictionary[fold.Parent_ID];
                    parentFolder.AddChild(fold);
                    //re save
                    foldersDictionary[fold.Parent_ID] = parentFolder;
                }


                foldersDictionary.Add(fold.ID, fold);
            }
        }
    }

}
